﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.SafetyManagementEntity.Model;
using KingRoad.EPM.Web.Areas.SafetyConstruction.ViewModels;
using KingRoad.EPM.Web.Areas.SafetyConstruction.SafetyConstructionFramework.Common;

namespace KingRoad.EPM.Web.Areas.SafetyConstruction.Controllers
{
    public class ScmAssessmentitemsController : BaseController
    {
        //
        // GET: /ScmAssessmentitems/
        /// <summary>
        /// 考核项首页 xtgl_khxgl
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(string Scd_ID)
        {
            string sql = " Ai_Type='" + Scd_ID + "' ";
            ViewBag.Type = Scd_ID;
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            var dicList = ScmAssessmentitemsUIRule.GetListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Ai_Order", out totalRecord, sql, "asc");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new ScmAssessmentitemsViewModel { TheAssessmentitemsList = dicList });
        }
        /// <summary>
        /// 考核项目-列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.Type = Request.Form["Type"];
            ViewBag.iput_name = Request.Form["iput_name"];

            string sql = " 1=1 ";
            if (ViewBag.iput_equipment != "")
                sql += " and Ai_Project like '%" + ViewBag.iput_name + "%' ";
            if (ViewBag.Type != "")
                sql += " and Ai_Type= '" + ViewBag.Type + "' ";

            var dicList = ScmAssessmentitemsUIRule.GetListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Ai_Order", out totalRecord, sql, "asc");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("List", new ScmAssessmentitemsViewModel { TheAssessmentitemsList = dicList });
        }
        public ActionResult Add(string type)
        {
            ScmAssessmentitemsViewModel model = new ScmAssessmentitemsViewModel();
            ScmAssessmentitemsInfo info = new ScmAssessmentitemsInfo();
            info.Ai_ID = Guid.NewGuid().ToString();
            info.Ai_Type = type;
            model.TheAssessmentitemsInfo = info;
            return View(model);
        }

        public JsonResult AddData(ScmAssessmentitemsViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheAssessmentitemsInfo.Ai_CreateTime = DateTime.Now;
                vm.TheAssessmentitemsInfo.Ai_CreateUserID = GetLoginUserId;
                vm.TheAssessmentitemsInfo.Ai_ModifyTime = DateTime.Now;
                vm.TheAssessmentitemsInfo.Ai_ModifyUserID = GetLoginUserId;
                ScmAssessmentitemsUIRule.InsertEntity(vm.TheAssessmentitemsInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功!",
                    navTabId = "SafetyConstruction.ScmAssessmentitems.List",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败!",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        public ActionResult Edit(string id)
        {
            ScmAssessmentitemsViewModel model = new ScmAssessmentitemsViewModel();
            model.TheAssessmentitemsInfo = ScmAssessmentitemsUIRule.GetInfoByID(new Guid(id));
            return View(model);
        }
        public JsonResult EditData(ScmAssessmentitemsViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheAssessmentitemsInfo.Ai_CreateTime = DateTime.Now;
                vm.TheAssessmentitemsInfo.Ai_CreateUserID = GetLoginUserId;
                vm.TheAssessmentitemsInfo.Ai_ModifyTime = DateTime.Now;
                vm.TheAssessmentitemsInfo.Ai_ModifyUserID = GetLoginUserId;
                ScmAssessmentitemsUIRule.UpdateEntity(vm.TheAssessmentitemsInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功!",
                    navTabId = "SafetyConstruction.ScmAssessmentitems.List",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败!",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        public JsonResult Delete(string ids)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                ScmAssessmentitemsUIRule.DeleteEntityByMultiStringID(ids);
            });
            dynamic jsonReturn = new ExpandoObject();
            //删除成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功!",
                    navTabId = "SafetyConstruction.ScmAssessmentitems.List",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败!",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
    }
}
